#pragma once
#include "GUI_Morph.h"
#include "Graphics.h"
#include "Indexed Linked List.h"
#include "guimaterials.h"

struct MenuObjects
{
	int	MO_SQUAREPANEL;
	int MO_BOTTOMDOCK;
	int MO_SQUAREBUTTON;
	int MO_ARROWBUTTON;
	int MO_SIZE;
};

#define NullID 0


class GUI
{
private:
	bool			needToUpdate;
	MenuObjects		M_Objects;
	ILL				M_List;
	char*			m_cszTexNames [sizeof(MenuObjects)/sizeof(int)];

	void InitTextures()
	{
		M_Objects.MO_SQUAREBUTTON = Graphics::Engine()->AddAsset("Resources\\Menus\\Button.png",NULL);
		M_Objects.MO_SQUAREPANEL  = Graphics::Engine()->AddAsset("Resources\\Menus\\MenuPanel.png",NULL);	//Will pass back an Id, that should be my texID

	};
	void InitMenus()
	{

		gui_morph * ToAddMenu = new gui_morph();

		ToAddMenu->InitAsMenu(M_Objects.MO_SQUAREPANEL,CheckTest, Quad(0,0,100,100));
		ToAddMenu->addTextBox(Quad(0,15,10,10),"Options Menu");
		ToAddMenu->addTextBox(Quad(0,30,10,10),"Lighting:");
		ToAddMenu->addTextBox(Quad(0,45,10,10),"Difficulty:");
		M_List.Add("MenuPanel",(void*)ToAddMenu);

	};
	void InitButtons()
	{
		gui_morph * ToAddButton = new gui_morph();
		point sqButtonDim = Graphics::Engine()->GetAssetDimensions(M_Objects.MO_SQUAREBUTTON);


		ToAddButton->InitAsButton(M_Objects.MO_SQUAREBUTTON, CheckTest, Quad(800,0,(int)sqButtonDim.x,(int)sqButtonDim.y), ExecuteTest);
		ToAddButton->addTextBox(Quad(800,0,100,100),"Cheez");
		M_List.Add("Button",(void*)ToAddButton);

		ToAddButton = new gui_morph();
		ToAddButton->InitAsButton(M_Objects.MO_SQUAREBUTTON, CheckTest, Quad(800,50,128,32), GUI_spawnEntity);
		ToAddButton->addTextBox(Quad(800,50,100,100),"+Enemy");
		M_List.Add("Button",(void*)ToAddButton);
	};
public:
	GUI(){needToUpdate = true;};
	~GUI()
	{
		Record* r = NULL;
		while(M_List.Get(0))
		{
			r = M_List.Remove_Last();
			delete ((gui_morph*)(r->Data));
			r = NULL;
		}
	};

	inline static GUI* Engine()      
	{
		static GUI engine;return &engine;
	}
	void Init()
	{
		InitTextures();
		InitMenus();
		InitButtons();
		Graphics::Engine()->AddIndexLinkedList(&M_List,1);
	};

	void CheckCollision(int x, int y)
	{
		point Clicked(x,y);

		Record * cursor = M_List.Get(0);
		while(cursor)
		{
			if(((gui_morph*)(cursor->Data))->GetAssetIndex() == M_Objects.MO_SQUAREBUTTON){
				((gui_morph*)(cursor->Data))->checkCollision(Clicked);
				needToUpdate = true;	//Something was changed, we need to .
			}
			cursor = cursor->Next;
		}

	};

	void Update()
	{
		if(needToUpdate)
		{
			Record * cursor = M_List.Get(0);
			while(cursor)
			{
				((gui_morph*)(cursor->Data))->update();
				cursor = cursor->Next;
			}
		}
		needToUpdate = false;
	};

};

